home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / cxref_1_4a.lha / func.c < prev    next >
C/C++ Source or Header  |  1997-12-07  |  11KB  |  418 lines

  1. /***************************************
  2.   $Header: /home/amb/cxref/RCS/func.c 1.17 1997/06/14 16:35:38 amb Exp $
  3.  
  4.   C Cross Referencing & Documentation tool. Version 1.4.
  5.  
  6.   Handle Function stuff.
  7.   ******************/ /******************
  8.   Written by Andrew M. Bishop
  9.  
  10.   This file Copyright 1995,96,97 Andrew M. Bishop
  11.   It may be distributed under the GNU Public License, version 2, or
  12.   any higher version.  See section COPYING of the GNU Public license
  13.   for conditions under which this file may be redistributed.
  14.   ***************************************/
  15.  
  16. /*+ Control the debugging information from this file. +*/
  17. #define DEBUG 0
  18.  
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22.  
  23. #ifdef AMIGA /* olsen */
  24. #include "amiga.h"
  25. #endif /* AMIGA */
  26.  
  27. #include "memory.h"
  28. #include "datatype.h"
  29. #include "cxref.h"
  30.  
  31. /*+ The current parsing options. +*/
  32. extern int option_xref;
  33.  
  34. /*+ The current file that is being processed. +*/
  35. extern File CurFile;
  36.  
  37. /*+ The name of the current file. +*/
  38. extern char* parse_file;
  39.  
  40. /*+ When in a header file include functions from that file (except inline functions). +*/
  41. extern int in_header;
  42.  
  43. /*+ The current function, this is initialised by the start of a possible declaration and maintained until all of the
  44.     arguments have been added and confirmation that it is a definition and not a prototype is seen. +*/
  45. static Function cur_func=NULL;
  46.  
  47. /*+ The list of function prototypes and the files that they are defined in. +*/
  48. static StringList2 prototypes=NULL;
  49.  
  50. static Function NewFunctionType(char *name,char *type);
  51.  
  52.  
  53. /*++++++++++++++++++++++++++++++++++++++
  54.   Function that is called when a function prototype is seen.
  55.  
  56.   char* name The name of the function.
  57.  
  58.   int in_a_function Whether the reference is from within a function or at the top level of the file.
  59.   ++++++++++++++++++++++++++++++++++++++*/
  60.  
  61. void SeenFunctionProto(char* name,int in_a_function)
  62. {
  63.  if(!(option_xref&XREF_FUNC))
  64.     return;
  65.  
  66. #if DEBUG
  67.  printf("#Func.c# Function prototype '%s'\n",name);
  68. #endif
  69.  
  70.  if(!in_a_function)
  71.    {
  72.     if(!prototypes)
  73.        prototypes=NewStringList2();
  74.     AddToStringList2(prototypes,name,parse_file,0,1);
  75.    }
  76.  else
  77.     AddToStringList(cur_func->protos,name,0,1);
  78. }
  79.  
  80.  
  81. /*++++++++++++++++++++++++++++++++++++++
  82.   Function that is called when a function declaration is seen.
  83.   This may or may not be a function defintion, we will need to wait and see.
  84.  
  85.   char* name The name of the function.
  86.  
  87.   int scope The scope of the function definition.
  88.   ++++++++++++++++++++++++++++++++++++++*/
  89.  
  90. void SeenFunctionDeclaration(char* name,int scope)
  91. {
  92. #if DEBUG
  93.  printf("#Func.c# Function declaration for '%s()'\n",name);
  94. #endif
  95.  
  96.  if(cur_func)
  97.     DeleteFunctionType(cur_func);
  98.  
  99.  cur_func=NewFunctionType(name,NULL);
  100.  
  101.  cur_func->comment=MallocString(GetCurrentComment());
  102.  cur_func->scope=scope;
  103.  
  104.  if(in_header)
  105.     cur_func->incfrom=MallocString(parse_file);
  106. }
  107.  
  108.  
  109. /*++++++++++++++++++++++++++++++++++++++
  110.   Called when a possible function definition is confirmed.
  111.  
  112.   char* type The type of the function, or NULL at the end of a definition.
  113.   ++++++++++++++++++++++++++++++++++++++*/
  114.  
  115. void SeenFunctionDefinition(char* type)
  116. {
  117.  Function *func=&CurFile->functions;
  118.  int i;
  119.  
  120.  if(cur_func->scope&INLINED && cur_func->incfrom)
  121.     return;
  122.  
  123. #if DEBUG
  124.  printf("#Func.c# Function definition %s for '%s()'\n",type?"start":"end",cur_func->name);
  125. #endif
  126.  
  127.  if(!type)
  128.    {cur_func=NULL;return;}
  129.  
  130.  cur_func->type=MallocString(type);
  131.  
  132.  cur_func->cret=MallocString(SplitComment(&cur_func->comment,type));
  133.  if(!cur_func->cret)
  134.     cur_func->cret=MallocString(GetCurrentComment());
  135.  
  136.  if(option_xref&XREF_FUNC)
  137.     if(prototypes)
  138.        for(i=0;i<prototypes->n;i++)
  139.           if(!strcmp(cur_func->name,prototypes->s1[i]))
  140.             {cur_func->protofile=MallocString(prototypes->s2[i]); break;}
  141.  
  142.  for(i=0;i<cur_func->args->n;i++)
  143.     if(strcmp(cur_func->args->s1[i],"void") && strcmp(cur_func->args->s1[i],"...") && !strchr(cur_func->args->s1[i],' '))
  144.       {
  145.        char *old=cur_func->args->s1[i];
  146.        cur_func->args->s1[i]=MallocString(ConcatStrings(2,"int ",old));
  147.        cur_func->args->s2[i]=MallocString(SplitComment(&cur_func->comment,cur_func->args->s1[i]));
  148.        Free(old);
  149.       }
  150.  
  151.  while(*func)
  152.    {
  153.     if(strcmp(cur_func->name,(*func)->name)<0)
  154.       {
  155.        Function temp=*func;
  156.        *func=cur_func;
  157.        cur_func->next=temp;
  158.        break;
  159.       }
  160.     func=&(*func)->next;
  161.    }
  162.  
  163.  if(!cur_func->next)
  164.     *func=cur_func;
  165. }
  166.  
  167. /*++++++++++++++++++++++++++++++++++++++
  168.   Function that is called when a function argument is seen in the current function declaration.
  169.  
  170.   char* name The name of the argument.
  171.  
  172.   char* type The type of the argument, or NULL if a traditional style function definition.
  173.   ++++++++++++++++++++++++++++++++++++++*/
  174.  
  175. void SeenFunctionArg(char* name,char *type)
  176. {
  177. #if DEBUG
  178.  printf("#Func.c# Function arg %s '%s' in %s()\n",name?name:"K&R",type?type:"K&R",cur_func->name);
  179. #endif
  180.  
  181.  if(name)
  182.     if(type)
  183.       {
  184.        int i;
  185.  
  186.        for(i=0;i<cur_func->args->n;i++)
  187.           if(!strcmp(cur_func->args->s1[i],name))
  188.             {
  189.              Free(cur_func->args->s1[i]);
  190.              cur_func->args->s1[i]=MallocString(type);
  191.              cur_func->args->s2[i]=MallocString(SplitComment(&cur_func->comment,type));
  192.              break;
  193.             }
  194.        if(i==cur_func->args->n)
  195.           AddToStringList2(cur_func->args,type,SplitComment(&cur_func->comment,type),0,0);
  196.  
  197.        if(!cur_func->args->s2[i])
  198.           cur_func->args->s2[i]=MallocString(GetCurrentComment());
  199.       }
  200.     else
  201.        AddToStringList2(cur_func->args,name,NULL,0,0);
  202. }
  203.  
  204.  
  205. /*++++++++++++++++++++++++++++++++++++++
  206.   Function that is called when a comment is seen, that may be in a function body.
  207.  
  208.   int SeenFuncIntComment Returns a true value if the comment was accepted as an function internal comment.
  209.  
  210.   char* comment The comment that has been seen.
  211.   ++++++++++++++++++++++++++++++++++++++*/
  212.  
  213. int SeenFuncIntComment(char* comment)
  214. {
  215.  if(!cur_func || !cur_func->type)
  216.     return(0);
  217.  
  218. #if DEBUG
  219.  printf("#Func.c# Function internal comment '%s' in %s()\n",comment,cur_func->name);
  220. #endif
  221.  
  222.  if(cur_func->comment)
  223.    {
  224.     char* c=cur_func->comment;
  225.  
  226.     cur_func->comment=MallocString(ConcatStrings(3,c,"\n\n",comment));
  227.     Free(c);
  228.    }
  229.  else
  230.     cur_func->comment=MallocString(comment);
  231.  
  232.  return(1);
  233. }
  234.  
  235.  
  236. /*++++++++++++++++++++++++++++++++++++++
  237.   Function that is called when a function call is seen in the current function.
  238.  
  239.   char* name The name of the function that is called.
  240.   ++++++++++++++++++++++++++++++++++++++*/
  241.  
  242. void SeenFunctionCall(char* name)
  243. {
  244.  if(!(option_xref&XREF_FUNC))
  245.     return;
  246.  
  247. #if DEBUG
  248.  printf("#Func.c# Function call for '%s()' in %s()\n",name,cur_func->name);
  249. #endif
  250.  
  251.  AddToStringList2(cur_func->calls,name,NULL,1,1);
  252. }
  253.  
  254.  
  255. /*++++++++++++++++++++++++++++++++++++++
  256.   Function that is called when a function or variable is referenced in the current function.
  257.  
  258.   char* name The name of the function or variable that is referenced.
  259.  
  260.   int in_a_function Whether the reference is from within a function or at the top level of the file.
  261.   ++++++++++++++++++++++++++++++++++++++*/
  262.  
  263. void CheckFunctionVariableRef(char* name,int in_a_function)
  264. {
  265.  Variable var =CurFile->variables;
  266.  Function func=CurFile->functions;
  267.  StringList2 sl=NULL;
  268.  
  269.  if(!(option_xref&(XREF_VAR|XREF_FUNC)))
  270.     return;
  271.  
  272.  if(IsAScopeVariable(name))
  273.     return;
  274.  
  275. #if DEBUG
  276.  printf("#Func.c# Function/Variable reference for '%s' in %s\n",name,in_a_function?cur_func->name:CurFile->name);
  277. #endif
  278.  
  279.  if(option_xref&XREF_VAR)
  280.     while(var)
  281.       {
  282.        if(!strcmp(var->name,name))
  283.          {
  284.           if(in_a_function)
  285.              sl=cur_func->v_refs;
  286.           else
  287.              sl=CurFile->v_refs;
  288.           break;
  289.          }
  290.        var=var->next;
  291.       }
  292.  
  293.  if(!sl && option_xref&XREF_FUNC)
  294.     while(func)
  295.       {
  296.        if(!strcmp(func->name,name))
  297.          {
  298.           if(in_a_function)
  299.              sl=cur_func->f_refs;
  300.           else
  301.              sl=CurFile->f_refs;
  302.           break;
  303.          }
  304.        func=func->next;
  305.       }
  306.  
  307.  if(!sl && option_xref&XREF_FUNC)
  308.    {
  309.     int i;
  310.     if(in_a_function)
  311.        for(i=0;i<cur_func->protos->n;i++)
  312.           if(!